package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.domain.db.DateData;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.vo.AnalysisSummaryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class AnalysisManager {

    @DubboReference
    private AnalysisByDayService analysisByDayService;

    // 统计展示
    public ResponseEntity findSummaryVo() {


        AnalysisSummaryVo vo = analysisByDayService.findAnalysisSummaryVo();

        return ResponseEntity.ok(vo);


    }
    //页面点线图每日分析
    public ResponseEntity findData(Long sd, Long ed, Integer type) {//todo
        Map<String, List<DateData>> map = new HashMap<>();

        Long day = (ed - sd) / 1000 / 60 / 60 / 24;
        List<DateData> thisYeardateDataList = new ArrayList<>();
        List<DateData> lastYearDateDataList = new ArrayList<>();
        for (int i = 0; i < day; i++) {
            String dateStr = DateUtil.offsetDay(new Date(sd), i).toDateStr();
            DateData dateData = new DateData();
            dateData.setTitle(dateStr);
            dateData.setAmount(0);
            thisYeardateDataList.add(dateData);
        }
        for (int i = 0; i < day; i++) {
            String lastDateStr = DateUtil.offsetDay(new Date(sd), i - 365).toDateStr();
            DateData dateData = new DateData();
            dateData.setAmount(0);
            dateData.setTitle(lastDateStr);
            lastYearDateDataList.add(dateData);
        }
        //1.获取今年的数据
        //1-1开始日期
        String startTime = DateUtil.formatDate(new Date(sd));
        //1-2结束日期
        String endTime = DateUtil.formatDate(new Date(ed));
        //获取数据
        List<AnalysisByDay> thisYearAnalist = analysisByDayService.findDateData(startTime, endTime);
        //2.获取去年的数据
        //2-1开始日期
        String lastYearStartTime = DateUtil.offsetDay(new Date(sd), -365).toDateStr();
        //2-2结束日期
        String lastYearEndTime = DateUtil.offsetDay(new Date(ed), -365).toDateStr();
        //获取数据
        List<AnalysisByDay> lastYearAnalist = analysisByDayService.findDateData(lastYearStartTime, lastYearEndTime);
        if (type == 101) {//查询新增
            if (CollectionUtil.isNotEmpty(thisYearAnalist)) {
                for (AnalysisByDay analysisByDay : thisYearAnalist) {
                    DateData dateData = new DateData();
                    dateData.setTitle(DateUtil.formatDate(analysisByDay.getRecordDate()));
                    dateData.setAmount(analysisByDay.getNumRegistered());
                    for (int i = 0; i < thisYeardateDataList.size(); i++) {
                        String title = thisYeardateDataList.get(i).getTitle();
                        String dateTitle = dateData.getTitle();
                        if (title.equals(dateTitle)) {

                            thisYeardateDataList.set(i, dateData);
                        }
                    }
                }
            }
            if (CollectionUtil.isNotEmpty(lastYearAnalist)) {
                for (AnalysisByDay analysisByDay : lastYearAnalist) {
                    DateData dateData = new DateData();
                    dateData.setTitle(DateUtil.formatDate(analysisByDay.getRecordDate()));
                    dateData.setAmount(analysisByDay.getNumRegistered());
                    for (int i = 0; i < lastYearDateDataList.size(); i++) {
                        if (lastYearDateDataList.get(i).getTitle().equals(dateData.getTitle())) {
                            lastYearDateDataList.set(i, dateData);
                        }
                    }
                }
            }
            map.put("thisYear", thisYeardateDataList);
            map.put("lastYear",lastYearDateDataList);
            return ResponseEntity.ok(map);
        } else if (type == 102) {//查询活跃用户
            if (CollectionUtil.isNotEmpty(thisYearAnalist)) {
                for (AnalysisByDay analysisByDay : thisYearAnalist) {
                    DateData dateData = new DateData();
                    dateData.setTitle(DateUtil.formatDate(analysisByDay.getRecordDate()));
                    dateData.setAmount(analysisByDay.getNumActive());
                    for (int i = 0; i < thisYeardateDataList.size(); i++) {
                        if (thisYeardateDataList.get(i).getTitle().equals(dateData.getTitle())) {
                            thisYeardateDataList.set(i, dateData);
                        }
                    }
                }
            }
            if (CollectionUtil.isNotEmpty(lastYearAnalist)) {
                for (AnalysisByDay analysisByDay : lastYearAnalist) {
                    DateData dateData = new DateData();
                    dateData.setTitle(DateUtil.formatDate(analysisByDay.getRecordDate()));
                    dateData.setAmount(analysisByDay.getNumActive());
                    for (int i = 0; i < lastYearDateDataList.size(); i++) {
                        if (lastYearDateDataList.get(i).getTitle().equals(dateData.getTitle())) {
                            lastYearDateDataList.set(i, dateData);
                        }
                    }
                }
            }
            map.put("thisYear", thisYeardateDataList);
            map.put("lastYear",lastYearDateDataList);
            return ResponseEntity.ok(map);
        } else {
            if (CollectionUtil.isNotEmpty(thisYearAnalist)) {
                for (AnalysisByDay analysisByDay : thisYearAnalist) {
                    DateData dateData = new DateData();
                    dateData.setTitle(DateUtil.formatDate(analysisByDay.getRecordDate()));
                    dateData.setAmount(analysisByDay.getNumRetention1d());
                    for (int i = 0; i < thisYeardateDataList.size(); i++) {
                        if (thisYeardateDataList.get(i).getTitle().equals(dateData.getTitle())) {
                            thisYeardateDataList.set(i, dateData);
                        }
                    }
                }
            }
            if (CollectionUtil.isNotEmpty(lastYearAnalist)) {
                for (AnalysisByDay analysisByDay : lastYearAnalist) {
                    DateData dateData = new DateData();
                    dateData.setTitle(DateUtil.formatDate(analysisByDay.getRecordDate()));
                    dateData.setAmount(analysisByDay.getNumRetention1d());
                    for (int i = 0; i < lastYearDateDataList.size(); i++) {
                        if (lastYearDateDataList.get(i).getTitle().equals(dateData.getTitle())) {
                            lastYearDateDataList.set(i, dateData);
                        }
                    }
                }
            }
            map.put("thisYear", thisYeardateDataList);
            map.put("lastYear",lastYearDateDataList);
            return ResponseEntity.ok(map);
        }
    }


}
